package com.aisier.network.observer

import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.MutableLiveData
import com.blankj.utilcode.util.ToastUtils
import com.zzq.my.jetpacks.net.entity.ApiResponse

 /*
   * ================================================
   * 描述：扩展LiveData，通过kotlin的DSL表达式替换java的callback回调，简写代码
   * 作者：zhuangzeqin
   * 时间: 2021/11/5-15:57
   * 邮箱：zzq@eeepay.cn
   * 备注:
   * ----------------------------------------------------------------
   * You never know what you can do until you try !
   *      _              _           _     _   ____  _             _ _
   *     / \   _ __   __| |_ __ ___ (_) __| | / ___|| |_ _   _  __| (_) ___
   *    / _ \ | '_ \ / _` | '__/ _ \| |/ _` | \___ \| __| | | |/ _` | |/ _ \
   *   / ___ \| | | | (_| | | | (_) | | (_| |  ___) | |_| |_| | (_| | | (_) |
   *  /_/   \_\_| |_|\__,_|_|  \___/|_|\__,_| |____/ \__|\__,_|\__,_|_|\___/
   *
   * 签名：最痛苦的事不是我失败了，而是我本可以.--zzq
   * ----------------------------------------------------------------
   * ================================================
   */
class StateLiveData<T> : MutableLiveData<ApiResponse<T>>() {

    fun observeState(owner: LifecycleOwner, listenerBuilder: ListenerBuilder.() -> Unit) {
//        val listener = ListenerBuilder().also(listenerBuilder)//
        //实例化回调接口实现类---延迟初始化 而且当且仅当变量被第一次调用的时候，委托方法才会执行
        val listener : ListenerBuilder by lazy { ListenerBuilder().apply(listenerBuilder) }
//        listener.listenerBuilder()

        val value = object : IStateObserver<T>() {

            override fun onError(e: Throwable) {
                listener.mErrorListenerAction?.invoke(e) ?: ToastUtils.showShort("Http Error")
            }

            override fun onDataEmpty() {
                listener.mEmptyListenerAction?.invoke()
            }

            override fun onComplete() {
                listener.mCompleteListenerAction?.invoke()
            }

            override fun onSuccess(code: Int?, message: String?, data: T?, count: Int?) {
                listener.mSuccessListenerAction?.invoke(code,message,data,count)
            }

            override fun onFailed(code: Int?, message: String?, data: T?, count: Int?) {
                listener.mFailedListenerAction?.invoke(code,message,data,count)
            }

        }
        super.observe(owner, value)
    }

    inner class ListenerBuilder {
        internal var mSuccessListenerAction: ((code: Int?, message: String?, data: T?, count: Int?) -> Unit)? = null
        internal var mErrorListenerAction: ((Throwable) -> Unit)? = null
        internal var mEmptyListenerAction: (() -> Unit)? = null
        internal var mCompleteListenerAction: (() -> Unit)? = null
        internal var mFailedListenerAction: ((code: Int?, message: String?, data: T?, count: Int?) -> Unit)? = null

        fun onSuccess(action: (code: Int?, message: String?, data: T?, count: Int?) -> Unit) {
            mSuccessListenerAction = action
        }

        fun onFailed(action: (code: Int?, message: String?, data: T?, count: Int?) -> Unit) {
            mFailedListenerAction = action
        }

        fun onException(action: (Throwable) -> Unit) {
            mErrorListenerAction = action
        }

        fun onEmpty(action: () -> Unit) {
            mEmptyListenerAction = action
        }

        fun onComplete(action: () -> Unit) {
            mCompleteListenerAction = action
        }
    }

}